package com.bktech.stupidnews.controlpanel;

import java.util.ArrayList;

import android.content.Context;
import android.content.res.Resources;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bktech.service.ImageDownloader;
import com.bktech.stupidnews.R;
import com.bktech.stupidnews.controlpanel.Category.TYPE;

public class MyExpanderListAdapter extends BaseExpandableListAdapter {
	LayoutInflater inflate;
	ArrayList<Category> listCategory;
	Resources resource;
	static ImageDownloader dowloader;

	public MyExpanderListAdapter(Context context, ArrayList<Category> data) {
		resource = context.getResources();
		dowloader = new ImageDownloader(context);
		inflate = LayoutInflater.from(context);
		listCategory = new ArrayList<Category>();
		listCategory.add(new Category(TYPE.BUTTON_HOME, "Home"));
		for (int i = 0; i < data.size(); i++) {
			listCategory.add(data.get(i));
		}
		listCategory.add(new Category(TYPE.BUTTON_ADD, "Add website"));
		listCategory.add(new Category(TYPE.BUTTON_EDIT, "Edit Content"));
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return listCategory.size();
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		return listCategory.get(groupPosition).listChild.size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return listCategory.get(groupPosition);
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return listCategory.get(groupPosition).listChild.get(childPosition);
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return groupPosition;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return groupPosition * 100 + childPosition;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		if (listCategory.get(groupPosition).getType() == TYPE.BUTTON_HOME) {
			convertView = inflate.inflate(R.layout.panel_setting_button_home,
					parent, false);
		} else if (listCategory.get(groupPosition).getType() == TYPE.BUTTON_ADD) {
			convertView = inflate.inflate(R.layout.panel_setting_button,
					parent, false);
		} else if (listCategory.get(groupPosition).getType() == TYPE.BUTTON_EDIT) {
			convertView = inflate.inflate(R.layout.panel_setting_button,
					parent, false);
		} else {
			convertView = inflate.inflate(R.layout.panel_setting_category,
					parent, false);
			ImageView im = (ImageView) convertView
					.findViewById(R.id.image_category);
			if (listCategory.get(groupPosition).expand) {
				im.setImageDrawable(resource
						.getDrawable(R.drawable.ic_action_next_item));
			} else {
				im.setImageDrawable(resource
						.getDrawable(R.drawable.ic_action_expand));
			}
		}

		TextView tv = (TextView) convertView.findViewById(R.id.text);
		tv.setText(listCategory.get(groupPosition).name);
		Log.d("GET", "getGroupView");
		return convertView;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = inflate.inflate(R.layout.panel_setting_rss, parent,
					false);
		}
		TextView tv = (TextView) convertView.findViewById(R.id.rss_text);
		tv.setText(listCategory.get(groupPosition).listChild.get(childPosition).title);
		if (listCategory.get(groupPosition).listChild.get(childPosition).imageUrl != null
				&& !listCategory.get(groupPosition).listChild
						.get(childPosition).imageUrl.equals("")) {
			ImageView imv = (ImageView) convertView
					.findViewById(R.id.image_child);
			dowloader.download(listCategory.get(groupPosition).listChild
					.get(childPosition).imageUrl, imv);
		}
		return convertView;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return true;
	}

	public Category getCategory(int pos) {
		return listCategory.get(pos);
	}

}
